Introduction to R

TU Dresden

Sarah Zeller

Artwork by Allison Horst

Why R?

  • beginner-friendly programming language
  • easy data-handling ✨
  • programmming language specifically for statistics
  • awesome for creating graphs 📈

Examples

Examples

Examples

So what can you do with R?

Simple calculations

Show data

Plot data

Things in R

  • Values: 1, "Dresden", "2024-12-10"
  • Objects: x <- c(22/7, 0.99, 3)
  • Functions: round(x, digits = 3

Data science workflow

Let’s get started! 🚀

  • Open RStudio
  • Create an R-project (.rproj)
  • Open a .R script
  • Save this script
  • Let’s check out the different panes 🔎

In the console…

Let’s use this as a calculator.

In the console (ctd)…

Let’s learn how to use a function. For this, we need to add arguments to the function call.

?head
?names
?plot

In the R script…

Let’s have a look at a data set: mtcars. What is it about?

To execute a line of code, use Shift + Enter.

Excursion: Classes

  • numeric (1.34, -10.6)
  • integer (1, -99)
  • character ("hello", "world")
  • NAs: “not assigned”
  • factors

Pop quiz

What class does this result have?

Pop quiz – NAs

What’s the result?

Let’s plot this data set, specifically the columns mpg and hp. To access them, we have to put a $ between the name of the data set and the name of the column. It will show up in the Plots pane.

Comments

We’re still in the R script. What if we want to explain to others (or our future self) what we intended with this code?

Assigning (<-)

What if we want to create a new object? Easy:

Packages

What if we want more functions? We install packages, which contain functions. We install them once (in the console).

We can then load them (once per script).

Your turn!

Have a look at the dataset palmerpenguins. What is it about? Plot a scatterplot.

Artwork by Allison Horst